﻿using SHDocVw;
using SUNRPA.CONTROLS.IMPL.domain;
using SUNRPA.CONTROLS.IMPL.domain.constant;
using SUNRPA.CONTROLS.IMPL.domain.flowItemParam;
using SUNRPA.CONTROLS.IMPL.domain.flowRunningVar;
using SUNRPA.CONTROLS.IMPL.util;
using System.Collections.Concurrent;
using System.Text;
using System.Windows;
using System.Windows.Automation;

namespace SUNRPA.CONTROLS.IMPL.controls {
    /// <summary>
    /// 对于打开网页的超时错误处理暂时都没有做，之后在完善
    /// </summary>
    public class GetWinWindowObjNAction:BaseControlActions {
        public override int Execute(int currentIndex, FlowItem item, ConcurrentDictionary<string, ConcurrentDictionary<string, RPAVariableDefineWrapper>> globleData, List<FlowItem> flowItems) {
            GetWinWindowObjNParam? param = item.ControlParam as GetWinWindowObjNParam;
            string windowObj = param.WindowObj;
            WindowModel model = new WindowModel();

            int getWinMethod = param.GetWindowMethod;
            IntPtr winHandle = IntPtr.Zero;
            switch(getWinMethod) {
                case 0:
                    //标题或类名
                    string title = param.Title;
                    string className = param.ClassName;
                    this.GetStringVar(ref title, globleData);
                    this.GetStringVar(ref className, globleData);
                    if(param.IsPattern) {
                        //模糊匹配（取第一个）
                        User32Utils.EnumWindows(delegate(IntPtr hwnd, int lparm) {
                            if(hwnd == IntPtr.Zero || hwnd == null) { 
                                return 1;
                            }
                            AutomationElement ae = AutomationElement.FromHandle(hwnd);
                            string titleSub = ae.Current.Name;
                            string classNameSub = ae.Current.ClassName;
                            if((!string.IsNullOrEmpty(title) && titleSub.ToString().Contains(title))
                                || (!string.IsNullOrEmpty(className) && classNameSub.ToString().Contains(className))) {
                                winHandle = hwnd;
                                return 0;
                            } else { 
                                return 1;
                            }
                        }, 0);
                    } else { 
                        if(string.IsNullOrEmpty(title)) { 
                            title = null;
                        }
                        if(string.IsNullOrEmpty(className)) { 
                            className = null;
                        }
                        winHandle = User32Utils.FindWindow(className, title);
                    }
                    break;
                case 1:
                    //捕获窗口元素
                    WinElement winEle = param.WindowEle;
                    if(!winEle.ControlType.Equals(ControlType.Window) && !winEle.ControlType.Equals(ControlType.Pane)) { 
                        throw new RpaRuntimeException("元素类型错误，不是窗口元素");
                    }
                    winHandle = User32Utils.FindWindow(winEle.ClassName, null);
                    break;
                case 2:
                    //当前激活窗口
                    winHandle = User32Utils.GetForegroundWindow();
                    break;
                case 3:
                    //桌面
                    winHandle= User32Utils.GetDesktopWindow();
                    break;
                default:
                    throw new RpaRuntimeException("获取窗口方式错误！");
            }
            if(winHandle == null || winHandle == IntPtr.Zero) {
                throw new RpaRuntimeException($"没有找到窗口");
            }
            model.WinPtr = winHandle;
            this.saveVar(globleData, windowObj, model, DefaultContents.RUNNING_PARAM);
            return currentIndex;
        }
    }
}
